<template>
    <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart',
        },
        width: {
            type: String,
            default: '100%',
        },
        height: {
            type: String,
            default: '300px',
        },
        chartData: {
            type: Object,
            default: {},
        },
        materialName: {
            type: String,
            default: '',
        },
    },
    watch: {
        chartData(val) {
            this.$nextTick(() => {
                this.initChart(val)
            })
        },
    },
    data() {
        return {
            chart: null,
        }
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart(val) {
            this.chart = echarts.init(this.$el, 'macarons')
            let chartData = { ...val }
            let _this = this
            let colors = ['#5470C6', '#EC808D', '#95F204']
            this.chart.setOption({
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        // Use axis to trigger tooltip
                        type: 'line', // 'shadow' as default; can also be 'line' or 'shadow'
                    },
                },
                color: colors,
                toolbox: {
                    show: true,
                    feature: {
                        mark: {
                            show: true,
                        },
                        dataView: {
                            show: true,
                            readOnly: false,
                        },
                        magicType: {
                            show: true,
                            type: ['line', 'bar'],
                        },
                        restore: {
                            show: true,
                        },
                        saveAsImage: {
                            show: true,
                        },
                    },
                },
                legend: {
                    //	    top: 'bottom'
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true,
                },
                xAxis: {
                    type: 'category',
                    data: chartData.date,
                },
                yAxis: [
                    {
                        type: 'value',
                        name: '单位：',
                        axisLine: {
                            onZero: false,
                            show: true,
                            lineStyle: {
                                // color: colors[0]
                            },
                        },
                        // min: 0,
                        max: chartData.Ymax1,
                        // interval: 50,
                        axisLabel: {
                            formatter: '{value} ',
                        },
                    },
                    {
                        type: 'value',
                        name: '单位:百万',
                        // min: 0,
                        max: chartData.Ymax2,
                        // interval: 20,
                        axisLine: {
                            onZero: false,
                            show: true,
                            lineStyle: {
                                // color: colors[1]
                            },
                        },
                        axisLabel: {
                            formatter: '{value}百万',
                        },
                    },
                ],
                series: [
                    {
                        name: '接受',
                        type: 'bar',
                        stack: 'total',
                        label: {
                            show: true,
                        },
                        emphasis: {
                            focus: 'series',
                        },
                        data: chartData.acceptList,
                    },
                    {
                        name: '让步',
                        type: 'bar',
                        stack: 'total',
                        label: {
                            show: true,
                        },
                        emphasis: {
                            focus: 'series',
                        },
                        data: chartData.concessionAcceptanceList,
                    },
                    {
                        name: '实验物料',
                        type: 'bar',
                        stack: 'total',
                        label: {
                            show: true,
                        },
                        emphasis: {
                            focus: 'series',
                        },
                        data: chartData.materialsList,
                    },
                    {
                        name: '不接受',
                        type: 'bar',
                        stack: 'total',
                        label: {
                            show: true,
                        },
                        emphasis: {
                            focus: 'series',
                        },
                        data: chartData.unacceptList,
                    },
                    {
                        name: '挑选使用',
                        type: 'bar',
                        stack: 'total',
                        label: {
                            show: true,
                        },
                        emphasis: {
                            focus: 'series',
                        },
                        data: chartData.chooseList,
                    },
                    // {
                    // 	name: 'PPM(上线不合格数量/(来料数量-退货数量))*一百万',
                    // 	type: 'line',
                    // 	yAxisIndex: 1,
                    // 	itemStyle : { normal: {label : {show: true}}},
                    // 	// label: labelOption,
                    // 	tooltip: {
                    // 		valueFormatter: function(value) {
                    // 			return value ;
                    // 		}
                    // 	},
                    // 	emphasis: {
                    // 		focus: 'series'
                    // 	},
                    // 	data: chartData.ppmList,
                    // 	markLine: {
                    //         label: {
                    //             position:'middle',
                    //             formatter: '{b}: {c}',
                    //             show: true
                    //         },
                    //         lineStyle: {
                    //             color: '#D9001B'
                    //         },
                    //         data: _this.materialName ? [{
                    //             name: `${ _this.materialName }上线合格率`,
                    //             yAxis: chartData.percentPass,
                    //             label:{
                    //                 formatter: '{b}: {c}百万'
                    //             }
                    //         }] : []
                    //     }
                    // }
                ],
            })
        },
    },
}
</script>
